﻿using System;
//b4,b5
namespace ConsoleApp_b4_b5
{    
    //形状抽象类
    public abstract class Shape
    {
        private string name;
        public Shape(string s)
        {
            name = s;
        }
        public string Name {//形状的类型
            get {
                return name;
            }
            set {
                name = value;
            } 
        }
        public abstract double Area//面积抽象属性
        {
            get;
        }
        public virtual void Display() { }//展示，虚方法
    }
    public class Square : Shape
    {
        private int side;//边长字段
        public Square(int d,string n):base(n)
        {
            side = d;
        }
        public int Side//边长，属性
        {
            get
            {
                return side;
            }
            set
            {
                if (value > 0) side = value;
                else
                {
                    Console.WriteLine("错误，边长必须大于0，请重新输入");
                    side = Convert.ToInt32(Console.ReadLine());
                }
            }
        }
        public override double Area//实现面积
        {
            get
            {
                return side * side;
            }
        }
        public override void Display()//覆盖展示方法
        {
            Console.WriteLine("Shape:"+Name+",Side:"+Side+",Area:"+Area);
        }
    }
    public class Rectangle : Shape
    {
        private int width;//长
        private int height;//宽
        public Rectangle(int w,int h,string n) : base(n)
        {
            width = w;
            height = h;
        }
        public int Width//宽属性
        {
            get
            {
                return width;
            }
            set
            {
                if (value > 0) width = value;
                else
                {
                    Console.WriteLine("错误，宽必须大于0，请重新输入");
                    width = Convert.ToInt32(Console.ReadLine());
                }
            }
        }
        public int Height//长属性
        {
            get
            {
                return height;
            }
            set
            {
                if (value > 0) height = value;
                else
                {
                    Console.WriteLine("错误，长必须大于0,请重新输入");
                    height = Convert.ToInt32(Console.ReadLine());
                }
            }
        }
        public override double Area//实现面积
        {
            get
            {
                return width * height;
            }
        }
        public override void Display()//覆盖展示方法
        {
            Console.WriteLine("Shape:" + Name + ",Width:" + Width +",Height:"+Height+",Area:" + Area);
        }
    }
    public class Triangle : Shape
    {
        private int side1;//第一条边
        private int side2;//第二条边
        private int side3;//第三条边
        public Triangle(int s1, int s2,int s3, string n) : base(n)
        {
            side1 = s1;
            side2 = s2;
            side3 = s3;
        }
        public int Side1//第一条边属性
        {
            get
            {
                return side1;
            }
            set
            {
                if (value > 0) side1 = value;
                else
                {
                    Console.WriteLine("错误，边必须大于0，请重新输入");
                    side1 = Convert.ToInt32(Console.ReadLine());
                }
            }
        }
        public int Side2//第二条边属性
        {
            get
            {
                return side2;
            }
            set
            {
                if (value > 0) side2 = value;
                else
                {
                    Console.WriteLine("错误，边必须大于0，请重新输入");
                    side2 = Convert.ToInt32(Console.ReadLine());
                }
            }
        }
        public int Side3//第三条边属性
        {
            get
            {
                return side3;
            }
            set
            {
                if (value > 0) side3 = value;
                else
                {
                    Console.WriteLine("错误，边必须大于0，请重新输入");
                    side3 = Convert.ToInt32(Console.ReadLine());
                }
            }
        }
        public override double Area//实现面积
        {
            get
            {
                double p = (side1 + side2 + side3) / 2;
                return Math.Sqrt(p*(p - side1)*(p - side2)*(p - side3));
            }
        }
        public bool If_Legal_Triangle()
        {
            if (Math.Abs(side1 - side3) < side2 && side2 < side1 + side3)
            {
                return true;
            }
            else return false;
        }
        public override void Display()//覆盖展示方法
        {
            Console.WriteLine("Shape:" + Name + ",Side1:" + Side1 + ",Side2:" + Side2 + ",Side3:" + Side3 + ",Area:" + Area);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Random rd = new Random();
            for(int i = 0; i < 10; i++)
            {
                int sel = rd.Next(1, 4);
                Console.WriteLine((i+1)+".");
                switch (sel)
                {
                    case 1:
                        int d = rd.Next(1, 101);
                        Square square=new Square(d,"Square");
                        square.Display();
                        break;
                    case 2:
                        int a = rd.Next(1, 101);
                        int b = rd.Next(1, 101);
                        Rectangle rectangle = new Rectangle(a, b, "Rectangle");
                        rectangle.Display();
                        break;
                    case 3:
                        int a1 = rd.Next(1, 101);
                        int b1 = rd.Next(1, 101);
                        int c1 = rd.Next(Math.Abs(a1-b1)+1,a1+b1);
                        Triangle triangle = new Triangle(a1, b1, c1, "Triangle");
                        triangle.Display();
                        break;
                }
                  
            }
        }
    }
}
